﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Revis.VClinicNet.UI.External.Client.Service;

namespace Revis.VClinicNet.UI.External.Client.ViewModel
{
    public class ProfileViewModel : WorkspaceControlViewModelBase<View.Profile>
    {
        private PatientDto _patient = null;

        public ProfileViewModel()
        {
            _patient = new PatientDto() {
                Personal = new PersonalDataDto() {
                    Address = new AddressDto()
                }
            };
        }

        public override void Run()
        {
            Request();
        }

        private void Request()
        {
            ChangeState("Pobieranie informacji o pacjencie", RequestState.Requesting);
            _patientService.BeginProfile(new AsyncCallback(RequestEnded), null);
        }

        public void RequestEnded(IAsyncResult result)
        {
            try {
                Patient = _patientService.EndProfile(result);
                ChangeState("Informacje pobrane", RequestState.Success);
            } catch (Exception) {
                ChangeState(
                    "Wystąpił poważny błąd podczas pobierania informacji profilowych",
                    RequestState.Faulted);
            }
        }

        public PatientDto Patient
        {
            get { return _patient; }
            set
            {
                _patient = value;
                RaisePropertyChanged("FirstName");
                RaisePropertyChanged("Surname");
                RaisePropertyChanged("Pesel");
                RaisePropertyChanged("StreetName");
                RaisePropertyChanged("HomeNumber");
                RaisePropertyChanged("Telephone");
                RaisePropertyChanged("City");
                RaisePropertyChanged("Municipality");
                RaisePropertyChanged("PostalCode");
            }
        }

        public string FirstName { get { return _patient.Personal.FirstName; } set { } }
        public string Surname { get { return _patient.Personal.Surname; } set { } }
        public string Pesel { get { return _patient.Personal.Pesel; } set { } }
        public string StreetName { get { return _patient.Personal.Address.StreetName; } set { } }
        public string HomeNumber { get { return _patient.Personal.Address.HomeNumber; } set { } }
        public string Telephone { get { return _patient.Personal.Address.Telephone; } set { } }
        public string City { get { return _patient.Personal.Address.City; } set { } }
        public string Municipality { get { return _patient.Personal.Address.Municipality; } set { } }
        public string PostalCode { get { return _patient.Personal.Address.PostalCode; } set { } }
    }
}
